<template>
  <div class="tag">
    <div class="header">
      <div class="total">标签总数:{{total}}</div>
      <el-button type="primary" @click="openNewTag">新建标签</el-button>
    </div>
    <el-table :data="tableData" height="calc(100% - 110px)" style="width: 100%">
      <el-table-column prop="tag_name" label="标签名称"></el-table-column>
      <el-table-column prop="username" label="创建者"></el-table-column>
      <el-table-column prop="create_time" label="创建时间">
        <template
          slot-scope="scope"
        >{{ scope.row.create_time ? dayjs(Number(scope.row.create_time)).format('YYYY-MM-DD HH:mm:ss') : '-'}}</template>
      </el-table-column>
      <el-table-column prop="update_time" label="更新时间">
        <template
          slot-scope="scope"
        >{{ scope.row.update_time ? dayjs(Number(scope.row.update_time)).format('YYYY-MM-DD HH:mm:ss') : '-'}}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="openDelete(scope.row)" type="danger" size="small">删除</el-button>
          <el-button @click="openEdit(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="pageSize"
      :current-page="page"
      @current-change="pageChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>

    <el-dialog :title="isEdit ? '编辑标签' : '新建标签'" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="标签名称" :label-width="formLabelWidth">
          <el-input v-model="tag_name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="isEdit ? updateClick() : addClick()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import dayjs from "dayjs";
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("admin");
export default {
  data() {
    return {
      isEdit: false,
      dialogFormVisible: false,
      formLabelWidth: "120px",
      tag_name: "",

      pageSize: 10,
      page: 1,
      tag_id: 1,
      tableData: [],
      total: 0
    };
  },
  mounted() {
    this.fetchTagList();
  },
  methods: {
    dayjs,
    ...mapActions(["addTag", "getTaglist", "updateTag", "deleteTag"]),
    handleClose() {
      this.dialogFormVisible = false;
      this.dialogVisible = false;
    },
    openNewTag() {
      this.isEdit = false;
      this.tag_name = "";
      this.dialogFormVisible = true;
    },
    openDelete(row) {
      this.$confirm("确认删除？")
        .then(() => {
          this.tag_id = row.tag_id;
          this.deleteClick();
        })
        .catch(() => {});
    },
    openEdit(row) {
      this.isEdit = true;
      this.tag_name = row.tag_name;
      this.tag_id = row.tag_id;
      this.dialogFormVisible = true;
    },
    addClick() {
      const userInfoStr = localStorage.getItem("userInfo");
      const userInfo = JSON.parse(userInfoStr);
      const params = {
        tag_name: this.tag_name,
        user_id: userInfo.user_id || 1,
        username: userInfo.username || ""
      };
      this.addTag(params).then(() => {
        this.dialogFormVisible = false;
        this.page = 1;
        this.fetchTagList();
      });
    },
    updateClick() {
      const userInfoStr = localStorage.getItem("userInfo");
      const userInfo = JSON.parse(userInfoStr);
      const params = {
        tag_name: this.tag_name,
        tag_id: this.tag_id,
        user_id: userInfo.user_id || 1,
        username: userInfo.username || ""
      };
      this.updateTag(params).then(() => {
        this.dialogFormVisible = false;
        this.fetchTagList();
      });
    },
    deleteClick() {
      this.deleteTag(this.tag_id).then(() => {
        this.fetchTagList();
      });
    },
    fetchTagList() {
      this.getTaglist().then(res => {
        this.tableData = res.list;
        this.total = res.total;
      });
    },
    pageChange(res) {
      this.page = res;
    }
  }
};
</script>

<style lang="scss" scoped>
.tag {
  height: 100%;
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 64px;
    overflow-x: auto;
  }
  .el-table {
    //   height: calc(100% - 110px);
    //   overflow: hidden;
    // /deep/  .el-table__body-wrapper {
    //     height: calc(100% - 48px);
    //     overflow: auto;
    //   }
  }
}
</style>